





<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/base/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>${systemOption.systemTitle}</title>
    <%@ include file="/base/head.jsp"%>
    <script type="text/javascript" charset="UTF-8">
	$(function(){
    		$('#linkGrouptree').tree({
    			onClick: function(node){
		    			$("#linkManList").datagrid({title:' '+node.text});			    					    			
		    			$('#linkManList').datagrid({url:'${ctx}/json/listGsLinkMan.action?id='+node.id});
		    			$('#linkManList').datagrid('load');	    		
				}    			
    		});
	    	 $('#addname').combotree({
		           editable:false,
		           panelHeight:'200',
		           panelWidth:'250',
		           url:'${ctx}/jsonCustomer/customerCommonAction!listUserByDS.action',
				   onClick:function(node) {
		       		  var b =  $(this).tree('isLeaf', node.target);
		      		  if(!b) {
		      			$('#addname').combotree('setValue','');
		      			return;
		      		 }
		           } 
		   }); 
		   
		    $('#editorName').combotree({
		           editable:false,
		           panelHeight:'200',
		           panelWidth:'250',
		            url:'${ctx}/jsonCustomer/customerCommonAction!listUserByDS.action',
				   onClick:function(node) {
		       		  var b =  $(this).tree('isLeaf', node.target);
		      		  if(!b) {
		      			$('#editorName').combotree('setValue','');
		      			return;
		      		 }
		           }
		   });
    	});
    //增加
  	function add_group(){
  			$('#groupAddWnd').show();
            $('#groupAddWnd').dialog({
            	iconCls:'icon-add',
            	title:'增加联系组',
            	modal:true,
            	buttons: [{
				text:'确定',
				iconCls:'icon-ok',
				handler:function(){
					$('#groupAddForm').form('submit',{
					    success: function(data){
					        if(data=='true'){
					            $.messager.alert('友情提示','增加成功','info'); 
					            $('#linkGrouptree').tree('reload');
					        }else{
					        	$.messager.alert('友情提示','增加失败','error'); 
					        }
					        $('#groupAddWnd').dialog('close');
					    }
					});
				}
			},{
				text:'关闭',
				iconCls:'icon-cancel',
				handler:function(){
					$('#groupAddWnd').dialog('close');
				}
			}]
         });    
  		$('#groupAddForm').form('reset');
  		$('#firstBox').focus();  
  	}
  //修改
  function edit_group(){
  	var node = $('#linkGrouptree').tree('getSelected');
	if (!node){
		$.messager.alert('友情提示','请选择联系组','error');
	}else{
		$('#groupUpdateWnd').show();
		$('#groupUpdateWnd').dialog({
        iconCls:'icon-edit',
        title:'修改联系组',
        modal:true,
        buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function(){
					$('#groupUpdateForm').form('submit',{
					    success: function(data){
					        if(data=='true'){
					            $.messager.alert('友情提示','修改成功','info');
					            $('#linkGrouptree').tree('reload');
					        }else{
					        	$.messager.alert('友情提示','修改失败','error');
					        }
					        $('#groupUpdateWnd').dialog('close');
					    }
					});
				}
			},{
				text:'关闭',
				iconCls:'icon-cancel',
				handler:function(){
					$('#groupUpdateWnd').dialog('close');
				}
		}]
            });
            //加载数据
            $.post("${ctx}/json/loadGsLinkGroup.action", {"id": node.id },
			   function(data){
			    if(data){
					$('#groupId').val(data.id);
					$('#grouptext').val(data.text);
					$('#groupsortnum').numberspinner('setValue',data.sortnum);
				}else{
					$.messager.alert('友情提示','加载失败','error');
				}
			   }, "json");


		}
	}
	function remove_group(){
		var node = $('#linkGrouptree').tree('getSelected');
		if (!node){
			$.messager.alert('友情提示','请选择联系组','error');
		}else{
			$.messager.confirm('友情提示','将删除该联系组及联系人，确定么？',function(r){   
				    if (r){   
				       $.post("${ctx}/json/removeGsLinkGroup.action", {"id": node.id },
					   function(data){
					    if(data==true){  
							$.messager.alert('友情提示','删除成功','info'); 
							$("#linkManList").datagrid({title:'数据字典 '});
							$('#linkManList').datagrid('loadData', { total: 0, rows: [] });
							$('#linkGrouptree').tree('reload');
						}else{
							$.messager.alert('友情提示','删除失败','error'); 
						}
					   }, "json");   
				    }   
				});
		}
	
	}
	//增加字典值
	function add_value(){
		var node=$('#linkGrouptree').tree('getSelected');
		if(node!=null){
	    		$('#manAddWnd').show();
	    		$('#manAddWnd').dialog({
	    			title: '增加联系人', 
					modal:true,
					buttons:[{
						text:'确定',
						iconCls:'icon-ok',
						handler:function(){
							var test = "";
							var nameIds = $('#addname').combotree('getValues');
					         for(var i=0;i<nameIds.length;i++)
						   	  {
						   		if(i>0){
						   			test+=',';
						   		}
						   		test+=nameIds[i];
						     }
						    $('#Addusername').val(test);
							$('#manAddForm').form('submit',{
							url:'${ctx}/json/saveGsLinkMan.action?man.group_id='+node.id,
					    	success: function(data){
						        if(data=='true'){
						            $.messager.alert('友情提示','增加成功','info');						            
						        }else{
						        	$.messager.alert('友情提示','增加失败','error');
						        }
						        $('#manAddWnd').dialog('close');
						        $('#linkManList').datagrid('reload');
					    	}
					});
						}
					},{
						text:'关闭',
						iconCls:'icon-cancel',
						handler:function(){$('#manAddWnd').dialog('close')}
					}]
	    		});
	    		$('#manAddForm').form('reset');
    	}else{
    			$.messager.alert('友情提示','请选择联系组','error');
    	}
	
	}
  //修改字典值
  function edit_value(){
  	var row = $('#linkManList').datagrid('getSelected');
	if (!row){
		$.messager.alert('友情提示','请选择联系人','error');
	}else{
		$('#manUpdateWnd').show();
		$('#manUpdateWnd').dialog({
        iconCls:'icon-edit',
        title:'修改字联系人',
        modal:true,
        buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function(){
					var nameIds = $('#editorName').combotree('getValue');
				    $('#editorusername').val(nameIds);
					$('#manUpdateForm').form('submit',{
						url:'${ctx}/json/saveGsLinkMan.action',
					    success: function(data){
					        if(data=='true'){
					            $.messager.alert('友情提示','修改成功','info');
					            $('#linkManList').datagrid('load');
					        }else{
					        	$.messager.alert('友情提示','修改失败','error');
					        }
					        $('#manUpdateWnd').dialog('close');
					    }
					});
				}
			},{
				text:'关闭',
				iconCls:'icon-cancel',
				handler:function(){
					$('#manUpdateWnd').dialog('close');
				}
		}]
            });
            //加载数据
			$('#manid').val(row.id);
			$('#groupid').val(row.group_id);
			$('#editorName').combotree('setValue',row.username);
			$('#editorName').combotree('setText',row.name);
			$('#phoneNum').val(row.phoneNum);
		}
	}
	function remove_value(){
		var row = $('#linkManList').datagrid('getSelected');
		if (!row){
			$.messager.alert('友情提示','请选择联系人','error');
		}else{
			$.messager.confirm('友情提示','将删除该联系人，确定么？',function(r){   
				    if (r){   
				       $.post("${ctx}/json/removeGsLinkMan.action", {"mid": row.id },
					   function(data){
					    if(data==true){  
							$.messager.alert('友情提示','删除成功','info'); 
							$('#linkManList').datagrid('load');
						}else{
							$.messager.alert('友情提示','删除失败','error'); 
						}
					   }, "json");   
				    }   
				});
		}
	
	}
    </script>
  </head>
  
  <body>
    <div class="easyui-layout" fit="true">
		<div data-options="region:'west',split:true" style="width:200px;border:0" >
			<div id="p" class="easyui-panel" title="联系组" data-options="fit:true,iconCls:'icon-group'">
				<div class="toolbar">
					<a href="javascript:void(0)" onclick="add_group()" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">增加</a>
					<a href="javascript:void(0)" onclick="edit_group()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a>
					<a href="javascript:void(0)" onclick="remove_group()" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true">删除</a>				
				</div>
				<ul id="linkGrouptree" class="easyui-tree" data-options="url:'${ctx}/json/listGsLinkGroup.action'"></ul>
			</div>		
		</div>
		<div data-options="region:'center'" style="border:0">
			<table id="linkManList" class="easyui-datagrid" title="联系人" 
				data-options="fit:true,singleSelect:true,toolbar:'#tb',fitColumns:true">
				<thead>
					<tr>
						<th data-options="field:'ck',checkbox:true"></th>
						<th data-options="field:'name',width:80">姓名</th>
						<th data-options="field:'id',hidden:true"></th>
						<th data-options="field:'group_id',hidden:true"></th>
					</tr>
				</thead>
			</table>		
		</div>
	</div>
	<!-- Datagrid工具栏 -->
	<div id="tb">
		<table width="100%" cellpadding="0" cellspacing="0">
		<tr>
		   <td class="toolDiv">  		
		  		<a href="javascript:void(0)" onclick="add_value()" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">增加</a><span class="vline">|</span>
				<a href="javascript:void(0)" onclick="edit_value()" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true">修改</a><span class="vline">|</span>			
				<a href="javascript:void(0)" onclick="remove_value()" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true">删除</a>
			</td>
		</tr>
		</table>
	</div> 
	<!-- 增加联系组窗口 -->
	<div id="groupAddWnd"  style="width: 310px; padding: 5px; background: #fafafa;display: none">
    	<form id="groupAddForm" method="post" action="${ctx }/json/saveGsLinkGroup.action">
    	<input type="hidden" name="group.user_id" value="${user.id }">
                <table cellpadding=3>                                        
                	<tr>
                        <td>联系组：</td>
                        <td><input id="firstBox" name="group.text" class="easyui-validatebox"  data-options="required:true" style="width:180px"/></td>
                    </tr>
                    <tr>
                        <td>排序：</td>
                        <td><input name="group.sortnum" class="easyui-numberspinner"  data-options="value:0,min:0,max:500,editable:false" style="width:185px"/></td>
					</tr>                   				                      
                </table>
        </form>
	</div>
   <!--修改联系组窗口-->
    <div id="groupUpdateWnd"  style="width: 310px; padding: 5px; background: #fafafa;display: none">
    	<form id="groupUpdateForm" method="post" action="${ctx }/json/saveGsLinkGroup.action">
    	<input id="groupId" name="group.id" type="hidden"/>
    	<input type="hidden" name="group.user_id" value="${user.id }">
                <table cellpadding=3>                                    
                	<tr>
                        <td>联系组：</td>
                        <td><input id="grouptext" name="group.text" class="easyui-validatebox"  data-options="required:true" style="width:180px"/></td>
                    </tr>
                    <tr>
                        <td>排序：</td>
                        <td><input id="groupsortnum" name="group.sortnum" class="easyui-numberspinner"  data-options="value:0,min:0,max:500,editable:false" style="width:185px"/></td>
					</tr>     				                      
                </table>
        </form>
    </div> 
    <!-- 增加联系人窗口 -->  
    <div id="manAddWnd"  style="width: 350px; padding: 5px; background: #fafafa;display: none;">
    	<form id="manAddForm" method="post">
                <table cellpadding=3>                                     
                	<tr>
                        <td>姓名：</td>
                        <td><input id="addname" style="width:250px;" name="man.name"
							class="easyui-combotree" data-options="required:true,onlyLeafCheck:true, multiple:true" />
                        </td>
                    	<td><s:hidden id="Addusername" name="man.username"/></td>
                    </tr>         				                      
                </table>
        </form>
	</div>
	<!-- 修改字典值窗口 -->  
    <div id="manUpdateWnd"  style="width: 350px; padding: 5px; background: #fafafa;display: none">
    	<form id="manUpdateForm" method="post">
    	<input id="manid" name="man.id" type="hidden"/>
    	<input id="groupid" name="man.group_id" type="hidden"/>
                <table cellpadding=3>                                        
                	<tr>
                        <td>姓名：</td>
                        <td><input id="editorName" style="width:250px;" name="man.name"
							class="easyui-combotree" data-options="required:true,onlyLeafCheck:true" />
							<s:hidden id="editorusername" name="man.username"/></td>
                    </tr>
                </table>
        </form>
	</div>
  </body>
</html>
